﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml;

namespace VoIPWeb.Uc
{
    public partial class ChangePassword : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                m_lbl_error.Visible = false;
            }
        }

        #region Private Methods
      
        #endregion

        #region Events
        protected void m_cmd_changepass_Click(object sender, EventArgs e)
        {
            try
            {
                string v_str_customer_acc = Session["Customer"] as string;
                var v_str_user = Session["User"] as string;
                var v_str_cpass_user = "samsung5568";
                var v_str_old_pass = m_txt_current_pass.Text;
                var v_str_new_pass = m_txt_new_pass.Text;
                if (!Session["Cpassword"].ToString().Equals(v_str_old_pass))
                {
                    string v_str_script = "";
                    v_str_script = "<script type='text/javascript'>alert('The current password you typed is not right!')</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "oncheckpass", v_str_script);
                    return;
                }
                string v_str_url_api = String.Format("https://www.voipinfocenter.com/api/request.ashx?command=changepassword&username={0}&password={1}&customer={2}&oldcustomerpassword={3}&newcustomerpassword={4}", v_str_user, v_str_cpass_user, v_str_customer_acc, v_str_old_pass, v_str_new_pass);

                HttpWebRequest v_http_webrep = (HttpWebRequest)WebRequest.Create(v_str_url_api);
                HttpWebResponse v_http_response = (HttpWebResponse)v_http_webrep.GetResponse();
                XmlDocument v_xmldocument = new XmlDocument();
                // Sau bước này, toàn bộ thông tin của file xml đầu ra đã được cho vào v_xmldocument
                v_xmldocument.Load(v_http_response.GetResponseStream());

                // Đọc dữ liệu để kiểm tra thông tin load đã thành công chưa?
                XmlNodeList v_xml_list = v_xmldocument.SelectNodes("//Result");
                // Nếu reset mật khẩu thành công
                if (v_xml_list[0].InnerText.Equals("Success"))
                {
                    m_lbl_result.Text = "Your password have been changed!";
                }
                else
                {
                    XmlNodeList v_xml_list_fail = v_xmldocument.SelectNodes("//Reason");
                    m_lbl_result.Text = v_xml_list_fail[0].InnerText;
                }
            }
            catch (Exception v_e)
            {
                m_lbl_error.Visible = true;
                m_lbl_error.Text = v_e.Message;
            }
        }

        protected void m_cmd_thoat_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/User/SettingsOverview.aspx", true);
            }
            catch (Exception v_e)
            {
                m_lbl_error.Visible = true;
                m_lbl_error.Text = v_e.Message;
            }
        }
        #endregion       
    }
}